/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.start.model;

import business.service.MongoDbFactory;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.star.common.PriorityPostData;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.log4j.Logger;
import org.bson.types.ObjectId;

/**
 *
 * @author Administrator
 */
public class PriorityPostModel {
    private static final String PriorityPost_COLLECTION = "priority_post";
    private static final Logger logger = Logger.getLogger(PriorityPostModel.class);
    private static final Lock createLock_ = new ReentrantLock();
    private static PriorityPostModel _instance = null;
    
    
    public static PriorityPostModel getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new PriorityPostModel();

                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }
    
    
    public PriorityPostModel() {
        try {
        } catch (Exception ex) {
            logger.error("PriorityPostModel: " + ex);
        }
    }
    
    
    public boolean updatePriorityPost(String idPost, short position, String user) {
        try{
            BasicDBObject basicDBObject = new BasicDBObject("position", position);
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(PriorityPost_COLLECTION);
            DBCursor cursor = coll.find(basicDBObject);
            if (cursor.hasNext()) {
                //update
                DBObject dbObject = cursor.next();                
                String objectId = ((ObjectId) dbObject.get("_id")).toString();
                BasicDBObject basicDBObject_update = new BasicDBObject()                    
                    .append("idPost", idPost)                               
                    .append("editor", user)                    
                    .append("updated", System.currentTimeMillis());
                BasicDBObject updateDBObject = new BasicDBObject("$set", basicDBObject_update);
                BasicDBObject searchDBQuery = new BasicDBObject("_id", new ObjectId(objectId));
                coll.update(searchDBQuery, updateDBObject);
            }
            else{
                //add new
                BasicDBObject basicDBObject_add = new BasicDBObject()                    
                    .append("idPost", idPost)
                    .append("position", position)                    
                    .append("editor", user)
                    .append("author", user)
                    .append("updated", System.currentTimeMillis())
                    .append("created", System.currentTimeMillis());
                coll.insert(basicDBObject_add);
            }
            return true;
        }catch (Exception ex) {
            logger.error("PriorityPostModel.updatePriorityPost: " + ex.getMessage(), ex);            
        }
        return false;
    }
    
}


